package day17.homework1;

import lombok.SneakyThrows;
import util.MyUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class TCPClient {
    @SneakyThrows
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 9527)) {// 连接服务端
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            while (true) {
                String inputMessage = MyUtils.input();
                writer.write(inputMessage + "\n");
                writer.flush();// 把缓冲区内容冲出去
                if(inputMessage.equals("byebye")) break;
                // 因为要收数据，所以用输入流接，从socket里面取接
                String message = reader.readLine();
                System.out.println("【客户端】收到消息：" + message);
                if ("byebye".equals(message)) break;
            }
        }
    }
}
